package com.shop2cn.iapi.sqoptrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.UpdateVoucherReq;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.UpdateVoucherCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_SqUpdateVoucher {

	private static UpdateVoucherReq updateVoucherReq;
	private static UpdateVoucherCall updateVoucherCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("支付凭证更新");
	}

	@Before
	public void caseUp() {
		updateVoucherReq = new UpdateVoucherReq();
		updateVoucherCall = new UpdateVoucherCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateVoucherCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public static void verify(TradingServiceResp resp) {

		for (Order order : resp.getOrderList()) {

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(order
					.getOrderId());

			Logger.verifyEquals(updateVoucherReq.getVoucherUrl(),
					orderPo.getVoucherOriginalUrl(), "验证VoucherOriginalUrl");
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateVoucher_001() {
		Logger.start(true, "创建一个线下支付订单，更新线下支付凭证成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder2(
					productBean, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			Long userId = resp.getUserId();
			
			// 同意线下支付的订单
			SqTradingCallService.payOrder(userId,
					resp.getTradingId(), "wxsqBuyer", "200");
			
			Thread.sleep(5000);

			updateVoucherReq.setVoucherUrl(TestDataManager.randomStr(10));
			updateVoucherReq.setMainOrderId(resp.getMainOrderId());
			updateVoucherReq.setUserId(userId);

			updateVoucherCall.setData(updateVoucherReq);
			updateVoucherCall.callService();

			verify(resp);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateVoucher_002() {
		Logger.start(true, "创建一个线下支付订单，多次更新线下支付凭证成功");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder2(
					productBean, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			Long userId = resp.getUserId();
			
			// 同意线下支付的订单
			SqTradingCallService.payOrder(userId,
					resp.getTradingId(), "wxsqBuyer", "200");
			
			Thread.sleep(5000);

			updateVoucherReq.setVoucherUrl(TestDataManager.randomStr(10));
			updateVoucherReq.setMainOrderId(resp.getMainOrderId());
			updateVoucherReq.setUserId(userId);

			updateVoucherCall.setData(updateVoucherReq);
			updateVoucherCall.callService();
			
			updateVoucherReq.setVoucherUrl(TestDataManager.randomStr(10));
			updateVoucherReq.setMainOrderId(resp.getMainOrderId());

			updateVoucherCall.setData(updateVoucherReq);
			updateVoucherCall.callService();

			verify(resp);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateVoucher_003() {
		Logger.start(true, "创建一个不支持线下支付订单，更新线下支付凭证失败");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder2(
					productBean, autotest_goodsOwnerId,
					productBean.getCurrency(), false);
			
			updateVoucherReq.setVoucherUrl(TestDataManager.randomStr(10));
			updateVoucherReq.setMainOrderId(resp.getMainOrderId());
			updateVoucherReq.setUserId(resp.getUserId());

			updateVoucherCall.setData(updateVoucherReq);
			updateVoucherCall.callService();

			Logger.verifyEquals(201, updateVoucherCall.getCode(), "验证code");
			Logger.verifyEquals(102, updateVoucherCall.getBCode(), "验证bcode");
			Logger.verifyEquals("还未确认是线下支付，不能更新凭证", updateVoucherCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateVoucher_004() {
		Logger.start(true, "创建一个支持线下支付订单，买家未同意线下支付，更新线下支付凭证失败");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder2(
					productBean, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			updateVoucherReq.setVoucherUrl(TestDataManager.randomStr(10));
			updateVoucherReq.setMainOrderId(resp.getMainOrderId());
			updateVoucherReq.setUserId(resp.getUserId());

			updateVoucherCall.setData(updateVoucherReq);
			updateVoucherCall.callService();

			Logger.verifyEquals(201, updateVoucherCall.getCode(), "验证code");
			Logger.verifyEquals(102, updateVoucherCall.getBCode(), "验证bcode");
			Logger.verifyEquals("还未确认是线下支付，不能更新凭证", updateVoucherCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UpdateVoucher_005() {
		Logger.start(true, "创建一个线下支付订单，输入错误的userId，更新线下支付凭证失败");
		try {
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder2(
					productBean, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			Long userId = resp.getUserId();
			
			// 同意线下支付的订单
			SqTradingCallService.payOrder(userId,
					resp.getTradingId(), "wxsqBuyer", "200");
			
			Thread.sleep(5000);

			updateVoucherReq.setVoucherUrl(TestDataManager.randomStr(10));
			updateVoucherReq.setMainOrderId(resp.getMainOrderId());
			updateVoucherReq.setUserId(userId + 1);

			updateVoucherCall.setData(updateVoucherReq);
			updateVoucherCall.callService();

			Logger.verifyEquals(201, updateVoucherCall.getCode(), "验证code");
			Logger.verifyEquals(103, updateVoucherCall.getBCode(), "验证bcode");
			Logger.verifyEquals("您无权操作该订单", updateVoucherCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
